package com.kly.manager;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.kly.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @date 2025/3/3
 */
@Component
public class CosManager {

    @Resource
    private COSClient cosClient;

    @Resource
    private CosClientConfig cosClientConfig;


    // 将本地文件上传到 COS
    public String uploadFile(String key, String fileName, File localFile) {
        String bucketName = cosClientConfig.getBucket();
        // 生成日期
        String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
        // 生成md5加密
        fileName = renameFile(fileName);
        int i = fileName.indexOf("?");
        if (i > 0) {
            fileName = fileName.substring(0, i);
        }
        String uploadPath = String.format("%s/%s/%s", key, dateStr, fileName);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, uploadPath, localFile);
        cosClient.putObject(putObjectRequest);
        return cosClientConfig.getHost() + "/" + uploadPath;
    }


    //      上传图片操作
    public PutObjectResult putPictureObject(String key, File localFile) {
        String bucketName = cosClientConfig.getBucket();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);

        // 对图片操作
        PicOperations picOperations = new PicOperations();
        // 返回原始信息  1
        picOperations.setIsPicInfo(1);

        // 图片压缩
        String webpKey = FileUtil.mainName(key) + ".webp";
        List<PicOperations.Rule> rules = new ArrayList<>();
        // 设置压缩规则
        PicOperations.Rule compressRule = new PicOperations.Rule();
        compressRule.setRule("imageMogr2/format/webp");
        compressRule.setFileId(webpKey);
        compressRule.setBucket(bucketName);
        rules.add(compressRule);

        // 设置缩略图规则
        if (localFile.length() > 2 * 1024) {
            PicOperations.Rule thumbnailRule = new PicOperations.Rule();
            thumbnailRule.setRule(String.format("imageMogr2/thumbnail/%sx%s", 256, 256));
            String thumbnailKey = FileUtil.mainName(key) + "_thumbnail." + FileUtil.getSuffix(key);
            thumbnailRule.setFileId(thumbnailKey);
            thumbnailRule.setBucket(bucketName);
            rules.add(thumbnailRule);
        }


        picOperations.setRules(rules);

        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }


    /**
     * 删除
     */
    public void deletePictureObject(String key) {
        // 移除字符串中的前缀
        String host = cosClientConfig.getHost();
        if (key.startsWith(host)) {
            key = key.substring(host.length() + 1);
        }
        cosClient.deleteObject(cosClientConfig.getBucket(), key);
    }


    /**
     * 文件重命名
     */
    private String renameFile(String fileName) {
        // 取后缀
        String suffix = FileUtil.getSuffix(fileName);
        // 生成唯一文件名
        String uuid = RandomUtil.randomString(16);

        return String.format("%s.%s", uuid, suffix);
    }
}
